
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>查看/修改权限</title>
    <script type="text/javascript" src="../../layuiadmin/js/system/jquery.min.js" ></script>
    <script src="../../layuiadmin/js/system/jquery.cookie.js"></script>
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/js/system/zTree/css/metroStyle/metroStyle.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../layuiadmin/js/system/zTree/js/jquery.ztree.all.min.js" ></script>
    <style>
        .ztree * {
            font-size: 16px;
            align-content: center;

        }
    </style>
</head>
<body>
<div>
    <button type="button" class="layui-btn layui-btn-sm" id="checkall">全选</button>
    <button type="button" class="layui-btn layui-btn-sm" id="InvertSelection">反选</button>
    <ul id="treeDemo" class="ztree"></ul>
    <button type="button" class="layui-btn " id="didClick">提交</button>
    <button class="layui-btn " data-type="test" id="goRolelist">返回上级</button>
</div>
<script>
    var settingss = {
        data: {
            simpleData: {
                enable: true,  //true 、 false 分别表示 使用 、 不使用 简单数据模式
                idKey: "authorityId",  //节点数据中保存唯一标识的属性名称
                pIdKey: "authorityPid",    //节点数据中保存其父节点唯一标识的属性名称
                rootPId: -1  //用于修正根节点父节点数据，即 pIdKey 指定的属性值
            },
            key: {
                name: "authorityName"  //zTree 节点数据保存节点名称的属性名称  默认值："name"
            }
        },
        check:{
            enable:true,  //true 、 false 分别表示 显示 、不显示 复选框或单选框
            nocheckInherit:true  //当父节点设置 nocheck = true 时，设置子节点是否自动继承 nocheck = true
        }
    };

    $(document).ready(function(){
        var tableRoleid=$.cookie("tableRoleid");
        var roleid=$.cookie("role_id");
        var oldId=[];
        $.ajax({
            type:"post",
            url:"http://localhost:8080/aut/listQx",
            xhrFields:{withCredentials: true},
            success:function(msg){
                zTreeObj = $.fn.zTree.init($("#treeDemo"), settingss, msg); //初始化树
                zTreeObj.expandAll(false);   //true 节点全部展开、false节点收缩
                $.post("http://localhost:8080/aut/updateAut",
                    {"tableRoleid":tableRoleid},function(msg){
                        for(var i=0;i<msg.length;i++){
                            oldId.push(msg[i].authorityId);
                            //设置选中节点
                            var node = zTreeObj.getNodeByParam("authorityId", msg[i].authorityId);
                            zTreeObj.checkNode(node, true, false);
                        }
                    });
            }
        });
        $('#didClick').click(function () {
            var ztreeObj=$.fn.zTree.getZTreeObj("treeDemo");
            var list=ztreeObj.getCheckedNodes();
            var autid=[];
            for(var i=0;i<list.length;i++){
                autid.push(list[i].authorityId);
            }
            var arr=autid.join(",");
            var brr=oldId.join(",");
            if(arr==brr){
                alert("您没有修改任何权限请返回或者修改权限");
            }else{
            $.post("http://localhost:8080/aut/insertAut",{
               "arr":arr,
                "tableRoleid":tableRoleid
            },function (msg) {
                if(msg==200){
                    if(roleid==tableRoleid) {
                        var con;
                        con = confirm("权限已更新是否重新登录"); //在页面上弹出对话框
                        if (con == true) {
                            window.history.go(-2);
                        } else {
                            window.location.href = "roleList.html";
                        }
                    }else{
                        window.location.href = "roleList.html";
                    }
                }
            })
            }
        });
        $("#goRolelist").click(function () {
            window.location.href=("roleList.html");
        })
        //全选and全不选
        $('#checkall').on('click',function (e) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");//得到tree对象 treeDemo是我的treeid
            var node = zTree.getNodes();//得到全部节点
            var nodes = zTree.transformToArray(node);//全部节点转换成数组array
            var checkNode = zTree.getCheckedNodes().length;//得到被选中的节点数量,zTree.getCheckedNodes()和zTree.getCheckedNodes(true)一样,可以传参(true/false),默认是true

            if (checkNode < nodes.length) {//已选中的节点数小于总数 - 全选
                $('#all').prop('checked', true)
                zTree.checkAllNodes(true);//给全部节点设置为true
            } else{
                zTree.checkAllNodes(false);//给全部节点设置为false
            }
        });
        //反选
        $("#InvertSelection").click(function (e) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");

            var checked = zTree.getCheckedNodes(true);//得到选中的节点
            var checkeds = zTree.transformToArray(checked); //转变为数组
            var checkNode = zTree.getCheckedNodes(true).length;//选中的节点数量

            var node = zTree.getNodes();//全部节点
            var nodes = zTree.transformToArray(node);//全部节点数量

            if(checkNode< nodes.length){//已选中的节点数小于总数 - 全选

                zTree.checkAllNodes(true);//给所有的都设置为true

                $.each(checkeds, function(index,node) {//之前选中的节点为false
                    zTree.checkNode(node, false, false);
                });

            }else{
                zTree.checkAllNodes(false);//否则所有的都设置为false
            }

        });
    });
</script>
</body>
</html>